﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TinCan.LRS.Web.Components;
using TinCan.LRS.Web.Filters;
using XAPI.Data;
using XAPI.Domain;

namespace TinCan.LRS.Web.Controllers
{
    public class StatementsController : xApiController
    {
        #region Fields
        IRepository _storageRepository;
        #endregion

        #region Contructors
        public StatementsController(IRepository storageRepository)
        {
            _storageRepository = storageRepository;
        }
        #endregion

        #region GET
        // GET api/statements
        public Statement Get()
        {
            return new Statement();
        }

        // GET api/statements/5
        public Statement Get(string statementId)
        {
            Guid id = new Guid();
            Guid.TryParse(statementId, out id);
            Statement statement = _storageRepository.SingleOrDefault<Statement>(s => s.id == id);
            return statement;
        }
        #endregion

        #region POST
        // POST api/statements
        public void Post([FromBody]List<Statement> statements)
        {
            foreach (var item in statements)
            {
                _storageRepository.Insert<Statement>(item);
            }
            //TODO: Implement POST method for Statements controller
        }

        #endregion

        #region PUT
        // PUT api/statements
        public Statement Put(string statementId, [FromBody]Statement statement)
        {
            Guid id = new Guid();
            if(Guid.TryParse(statementId, out id))
            {
                statement.id = id;
            }

            _storageRepository.Insert<Statement>(statement);

            return statement;
        }
        #endregion

        #region DELETE
        // DELETE api/statements/5
        public void Delete(int id)
        {
        }
        #endregion
    }
}
